<template>
  <el-card class="publish-container">
    <div slot="header" class="clearfix">
      <my-breadcrumb>发布文章</my-breadcrumb>
    </div>
    <!-- ref属性为了让vue可以找到这个组 -->
    <el-form ref="myForm" :rules="rules" :model="form" label-width="80px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="内容：" prop="content">
         <quill-editor v-model="form.content" :options="editorOption"></quill-editor>
      </el-form-item>
      <el-form-item label="封面">
        <el-radio-group v-model="form.coverType" @change="hImageTypeChange">
          <el-radio :label="1">单图</el-radio>
          <el-radio :label="3">三图</el-radio>
          <el-radio :label="0">无图</el-radio>
          <el-radio :label="-1">自动</el-radio>
        </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form.coverType>0">
            <el-row>
                <el-col :span="8" v-for="(item, index) in form.coverType" :key="item">
                    <my-cover v-model="form.coverImages[index]"></my-cover>
                </el-col>
            </el-row>
        </el-form-item>
      <my-channel v-model="form.channelId"></my-channel>
      <el-form-item>
          <el-button type="primary" @click="hSave">保存</el-button>
        </el-form-item>
    </el-form>

  </el-card>
</template>

<script>
// 导入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入组件
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'edit',
  data () {
    return {
      // 富文本配置对象
      editorOption: {
        // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      },
      form: {
        title: '',
        content: '',
        coverType: 0, // 封面类型：-1（自动）， 0（无图）， 1（1张图）， 3（3张图）
        coverImages: [], // 保存对应的封面图的地址
        channelId: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在5到30之间', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
          { min: 20, max: 30000, message: '最少20个字', trigger: 'blur' }
        ],
        channelId: [
          { required: true, message: '请选择频道', trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    quillEditor
  },
  created () {
    this.loadArticle()
  },
  methods: {
    async loadArticle () {
      // 1.发送请求获取数据
      const res = await this.$http({
        url: '/mp/v1_0/articles/' + this.$route.params.id,
        method: 'GET'
      })
      // 2. 保存数据，视图就更新了，页面也能显示出来了
      this.form.title = res.data.data.title
      this.form.content = res.data.data.content
      this.form.coverType = res.data.data.cover.type
      this.form.coverImages = res.data.data.cover.images
      this.form.channelId = res.data.data.channel_id
    },
    // 处理图片类型切换
    hImageTypeChange () {
      if (this.form.coverType === 1) {
        this.form.coverImages.length = 1
      } else if (this.form.coverType === 3) {
        this.form.coverImages.length = 3
      }
    },
    // 默认: 不是草稿
    hSave () {
      // 1. 验证数据有效性
      this.$refs.myForm.validate(valid => {
        if (valid) {
          this.doSave()
        }
      })
    },
    async doSave () {
      try {
        const { title, content, channelId, coverType, coverImages } = this.form
        // 2. 调用接口实现添加
        const res = await this.$http({
          method: 'PUT',
          url: '/mp/v1_0/articles/' + this.$route.params.id,
          data: {
            title,
            content,
            channel_id: channelId,
            cover: {
              type: coverType, // 先做无图的情况
              images: coverImages
            }
          }
        })
        console.log(res)
        this.$message.success('保存成功了')
        // 保存成功后跳转到内容管理
        this.$router.push('/articles')
      } catch (err) {
        console.log(err)
        this.$message.error('保存失败')
      }
    }
  }

}
</script>
<style scoped lang='less'>
// 在父组件中去设置子组件的样式，加上/deep/ 实现穿透功能
 .publish-container  /deep/ .ql-editor{
  height: 300px;
}
  .publish-container /deep/ .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
